package com.by.rpc.protocol;

import cn.hutool.core.util.StrUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 协议消息序列化枚举
 *
 * @author lzh
 */
@Getter
public enum ProtocolMessageSerializerEnum {

    /**
     * 序列化方式
     */
    JDK(0, "jdk"),
    JSON(1, "json"),
    KRYO(2, "kryo"),
    HESSIAN(3, "hessian");

    private final int key;

    private final String value;

    ProtocolMessageSerializerEnum(int key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 获取值列表
     *
     * @return 值列表
     */
    public static List<String> getValues() {
        return Arrays.stream(values()).map(ProtocolMessageSerializerEnum::getValue).collect(Collectors.toList());
    }

    /**
     * 根据key获取枚举
     *
     * @param key key
     * @return 枚举
     */
    public static ProtocolMessageSerializerEnum getEnumByKey(int key) {
        for (ProtocolMessageSerializerEnum serializerEnum : values()) {
            if (serializerEnum.key == key) {
                return serializerEnum;
            }
        }
        return null;
    }

    /**
     * 根据值获取枚举
     *
     * @param value 值
     * @return 枚举
     */
    public static ProtocolMessageSerializerEnum getEnumByValue(String value) {
        if (StrUtil.isEmpty(value)) {
            return null;
        }
        for (ProtocolMessageSerializerEnum serializerEnum : values()) {
            if (serializerEnum.value.equals(value)) {
                return serializerEnum;
            }
        }
        return null;
    }
}
